// OutputStream.cpp: implementation of the OutputStream class.
//
//////////////////////////////////////////////////////////////////////

#include "OutputStream.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

OutputStream::OutputStream()
{

}

OutputStream::~OutputStream()
{

}

void OutputStream::write(Mensaje& data){
	unsigned int length = sizeof(data);
	int result = send(socket,reinterpret_cast<char*>(&length),sizeof(length),0);
	if (result<=0){
		IOException ex("ERROR AL ENVIAR DATOS POR SOCKET",socket);
		closesocket(socket);
		throw ex;
	}
	result = send(socket,reinterpret_cast<char*>(&data),length,0);
	if (result<=0){
		IOException* ex = new IOException("ERROR AL ENVIAR DATOS POR SOCKET",socket);
		closesocket(socket);
		throw ex;
	}
}

void OutputStream::write(char* data, unsigned int length){
	int result = send(socket,data,length,0);
	while((result > 0)&&(result < length )){
			data+=result;
			length-=result;
			//Sleep(150);
			result = send(socket,data,length,0);
	}
	if ((socket != INVALID_SOCKET)&&(result == -1)){
		IOException* ex = new IOException("ERROR AL Enviar DATOS POR SOCKET",socket);
		closesocket(socket);
		throw ex;
	}
}
